<?php

namespace App\Service\User;

use App\Model\AccostLogs;
use App\Model\UserProfile;

/**
 * 声优列表
 */
class HostListService extends UserBaseService
{
    protected $pageNum = 30;

    public function handle()
    {
        $userId = $this->getUserId();
        $type = $this->request->input('type', 'hot');
        $page = $this->request->input('page', 1);
        $platform = $this->request->input('platform', 2);
        $userIds = $platform == 2 ? $this->getUserList($type) : $this->getIosUserList($type);
        $ret = ['list' => []];
        if (empty($userIds)) return $ret;
        $start = ($page - 1) * $this->pageNum;
        $userIds = array_slice($userIds, $start, $this->pageNum);
        $usersInfo = UserProfile::getUsersProfile($userIds, false, true);

        //获取搭讪的用户ID
        $accostLogs = AccostLogs::getLogByUid($userId);

        array_walk($usersInfo, function ($userProfile) use (&$ret,$accostLogs) {
            $item = UserProfile::buildHostItem($userProfile);
            //1-聊天，2-撩Ta
            $item['chat_type'] = is_array();
            $ret['list'][] = $item;
        });
        $this->packPhotos($ret['list'], $userIds);
        // array_multisort(array_column($ret['list'], 'online'), SORT_DESC, $ret['list']);
        // log_user_result($userId, "{$type}==" . json_encode($userIds));
        return $ret;
    }

    /**
     * 获取用户列表（不同类型）
     */
    function getUserList($type = 'hot')
    {
        $userIds = [];
        switch ($type) {
                //精选
            case 'hot':
                $onlineIds = UserProfile::getOnlineRecHosts(1);
                shuffle($onlineIds);
                $notOnlineIds = UserProfile::getOnlineRecHosts(0);
                shuffle($notOnlineIds);
                $userIds = array_merge($onlineIds, $notOnlineIds);
                break;
                //最新
            case 'new':
                $userIds = UserProfile::getNewUsers(UserProfile::ROLE_HOST);
                break;
                //其他
            case 'all':
                $userIds = UserProfile::getNewUsers(UserProfile::ROLE_HOST, 0);
                break;
        }
        return $userIds;
    }

    /**
     * IOS版本的
     */
    function getIosUserList()
    {
        return UserProfile::getNewUsers(UserProfile::ROLE_HOST);
    }
}
